SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE Procedure [dbo].[SP_TMDOCU_CLIE_I02]      
/*-------------------------------------------------------*/      
/*---Empresa              : OFISIS S.A.                --*/      
/*---Cliente              : OFISIS                 --*/      
/*---Sistema              : Tesoreria                  --*/      
/*---M©dulo               : Empresa                    --*/      
/*---Programa             : Grabaci©n Doc.Clientes     --*/      
/*---                       M®dulo Tesoreria y Ventas  --*/      
/*---Script               : tcmovi_b.sql               --*/      
/*---Nombre SP            : SP_TMDOCU_CLIE_I02         --*/      
/*---Desarrollado por     : Javier Flores A.           --*/      
/*---Fecha Creaci©n       : 14/03/2000                 --*/      
/*---Base Datos           : Microsoft Sql Server       --*/      
/*---Versi©n              : 7.0                        --*/      
/*---Invoca a SP          :                            --*/      
/*-------------------------------------------------------*/      
      
/*-------------------------------------------------------*/      
/*---Modificado 1 por     : Maribel Julca Rodriguez    --*/      
/*---Fecha Modificaci©n   : 21/04/2005                 --*/       
/*---Detalle Modificaci©n : ingreso de los campos CO_USUA_CREA, CO_USUA_MODI  --*/      
/*-------------------------------------------------------*/      
      
/*-------------------------------------------------------*/      
/*---Comentarios                                       --*/      
/*---                                                  --*/      
/*---                                                  --*/      
/*-------------------------------------------------------*/      
      
/*-------------------------------------------------------*/      
/*---Drop Proc SP_TMDOCU_CLIE_I02                      --*/      
/*---Grant Execute on SP_TMDOCU_CLIE_I02 to public     --*/       
/*---SP_TMDOCU_CLIE_I02                                --*/      
/*-------------------------------------------------------*/      
 
 As    
DECLARE       
@VSTI_CLIE  TD_VC_003,       
@VSNU_ASTO  TD_VC_020,      
@VSST_INTE_BACH TD_VC_001,    
@VSCO_TIPO_OPER TD_VC_003,    
@VSCO_TIPO_OPEG TD_VC_003,

 @ISCO_EMPR      TD_VC_002,       
 @ISCO_USUA_MODI TD_VC_008_USUA,      
 @ISCO_TIPO_DOCU TD_VC_003,       
 @ISNU_DOCU_CLIE TD_VC_020,       
 @ISCO_CLIE      TD_VC_020,      
 @ISNO_CLIE      TD_VC_100,      
 @ISCO_MONE      TD_VC_003,       
 @ISCO_TIPO_OPER TD_VC_003,       
 @ISCO_COND_PAGO TD_VC_003,        
 @IDFE_EMIS      TD_DT_001,       
 @ISCO_UNID_CONC TD_VC_003,       
 @IDFE_VENC      TD_DT_001,      
 @IDFE_PROG_COBR TD_DT_001,      
 @IDFE_DESP      TD_DT_001,      
 @INFA_TIPO_CAMB TD_NU_015_006,       
 @INIM_BRUT_AFEC TD_NU_016_004,      
 @INIM_BRUT_INAF TD_NU_016_004,  
 @INIM_BRUT_EXON TD_NU_016_004,            
 @INIM_GAFI      TD_NU_016_004,       
 @INIM_MORA      TD_NU_016_004,       
 @INIM_FLET      TD_NU_016_004,      
 @ISST_AFEC_GAFI TD_VC_001,       
 @ISST_AFEC_MORA TD_VC_001,       
 @ISST_AFEC_FLET TD_VC_001,       
 @ISST_DCT1_BRAF TD_VC_001,      
 @ISST_DCT1_BRIN TD_VC_001,  
 @ISST_DCT1_BREX TD_VC_001,            
 @ISST_DCT1_GAFI TD_VC_001,       
 @ISST_DCT1_MORA TD_VC_001,       
 @ISST_DCT1_FLET TD_VC_001,      
 @ISST_DCT2_BRAF TD_VC_001,       
 @ISST_DCT2_BRIN TD_VC_001,  
 @ISST_DCT2_BREX TD_VC_001,            
 @ISST_DCT2_GAFI TD_VC_001,       
 @ISST_DCT2_MORA TD_VC_001,      
 @ISST_DCT2_FLET TD_VC_001,       
 @INPC_DCT1      TD_NU_009_004,       
 @INIM_DCT1      TD_NU_016_004,       
 @INPC_DCT2      TD_NU_009_004,      
 @INIM_DCT2      TD_NU_016_004,        
 @ISCO_IMP1      TD_VC_003,       
 @INPC_IMP1      TD_NU_009_004,       
 @INIM_IMP1      TD_NU_016_004,       
 @ISCO_IMP2      TD_VC_003,      
 @INPC_IMP2      TD_NU_009_004,       
 @INIM_IMP2      TD_NU_016_004,       
 @ISCO_IMP3      TD_VC_003,       
 @INPC_IMP3      TD_NU_009_004,       
 @INIM_IMP3      TD_NU_016_004,      
 @INIM_TOTA      TD_NU_016_004,       
 @INIM_PAGA_REND TD_NU_016_004,       
 @ISCO_TIDO_ORIG TD_VC_003,       
 @ISNU_DOCU_ORIG TD_VC_020,      
 @IDFE_DOCU_ORIG TD_DT_001,       
 @ISDE_OBSE      TD_VC_100,       
 @ISCO_VEND		 TD_VC_020,    
 @ISCO_LIQU      TD_VC_020,      
 @ISTI_FACT      TD_VC_003,      
 @ISCO_MODE_DIST TD_VC_003,      
 @ISST_ASTO_RESU TD_VC_001       
          
Begin Tran SP_TMDOCU_CLIE_I02   

Select 
@ISCO_EMPR = CO_EMPR,
@ISCO_USUA_MODI = CO_USUA_MODI,
@ISCO_TIPO_DOCU = CO_TIPO_DOCU,
@ISNU_DOCU_CLIE = NU_DOCU_CLIE,
@ISCO_CLIE = CO_CLIE,
@ISNO_CLIE = NO_CLIE,
@ISCO_MONE = CO_MONE,
@ISCO_TIPO_OPER = CO_TIPO_OPER,
@ISCO_COND_PAGO = CO_COND_PAGO,
@IDFE_EMIS = FE_EMIS,
@ISCO_UNID_CONC = CO_UNID_CONC,
@IDFE_VENC = FE_VENC,
@IDFE_PROG_COBR = FE_PROG_COBR,
@IDFE_DESP = FE_DESP,
@INFA_TIPO_CAMB = FA_TIPO_CAMB,
@INIM_BRUT_AFEC = IM_BRUT_AFEC,
@INIM_BRUT_INAF = IM_BRUT_INAF,
@INIM_BRUT_EXON = IM_BRUT_EXON,
@INIM_GAFI  = IM_GAFI,
@INIM_MORA  = IM_MORA,
@INIM_FLET  = IM_FLET,
@ISST_AFEC_GAFI = ST_AFEC_GAFI,
@ISST_AFEC_MORA = ST_AFEC_MORA,
@ISST_AFEC_FLET = ST_AFEC_FLET,
@ISST_DCT1_BRAF = ST_DCT1_BRAF,
@ISST_DCT1_BRIN = ST_DCT1_BRIN,
@ISST_DCT1_BREX = ST_DCT1_BREX,
@ISST_DCT1_GAFI = ST_DCT1_GAFI,
@ISST_DCT1_MORA = ST_DCT1_MORA,
@ISST_DCT1_FLET = ST_DCT1_FLET,
@ISST_DCT2_BRAF = ST_DCT2_BRAF,
@ISST_DCT2_BRIN = ST_DCT2_BRIN,
@ISST_DCT2_BREX = ST_DCT2_BREX,
@ISST_DCT2_GAFI = ST_DCT2_GAFI,
@ISST_DCT2_MORA = ST_DCT2_MORA,
@ISST_DCT2_FLET = ST_DCT2_FLET,
@INPC_DCT1 = PC_DCT1,
@INIM_DCT1 = IM_DCT1,
@INPC_DCT2 = PC_DCT2,
@INIM_DCT2 = IM_DCT2,
@ISCO_IMP1 = CO_IMP1,
@INPC_IMP1 = PC_IMP1,
@INIM_IMP1 = IM_IMP1,
@ISCO_IMP2 = CO_IMP2,
@INPC_IMP2 = PC_IMP2,
@INIM_IMP2 = IM_IMP2,
@ISCO_IMP3 = CO_IMP3,
@INPC_IMP3 = PC_IMP3,
@INIM_IMP3 = IM_IMP3,
@INIM_TOTA = IM_TOTA,
@INIM_PAGA_REND = IM_PAGA_REND,
@ISCO_TIDO_ORIG = CO_TIDO_ORIG,
@ISNU_DOCU_ORIG = NU_DOCU_ORIG,
@IDFE_DOCU_ORIG = FE_DOCU_ORIG,
@ISDE_OBSE = DE_OBSE,
@ISCO_VEND = CO_VEND,
@ISCO_LIQU = CO_LIQU,
@ISTI_FACT = TI_FACT,
@ISCO_MODE_DIST = CO_MODE_DIST,
@ISST_ASTO_RESU = ST_ASTO_RESU

From #TWDOCU_CLIE_I01   
      
 Select @VSST_INTE_BACH = ST_INTE_BACH,    
   @VSCO_TIPO_OPER = CO_TIPO_OPER,    
   @VSCO_TIPO_OPEG = CO_TIPO_OPEG    
     
 From TMPARA_TESO Where CO_EMPR = @ISCO_EMPR      
     
     
 If Not Exists(Select CO_EMPR    
    From   TMDOCU_CLIE       
    Where  CO_EMPR = @ISCO_EMPR      
    And CO_CLIE = @ISCO_CLIE      
    And CO_TIPO_DOCU = @ISCO_TIPO_DOCU      
    And NU_DOCU_CLIE = @ISNU_DOCU_CLIE)       
 Begin      
     
     
  /*Busco el ultimo correlativo usado para insertar el nro de comprobante*/       
  If @VSST_INTE_BACH = 'N' AND @ISST_ASTO_RESU = 'N' And ( @VSCO_TIPO_OPER != @ISCO_TIPO_OPER  And       
@VSCO_TIPO_OPEG != @ISCO_TIPO_OPER  )    
  Begin    
   EXEC SP_TTULTI_ASTO_Q01 @ISCO_EMPR,@ISCO_USUA_MODI, @ISCO_UNID_CONC, @ISCO_TIPO_OPER, @IDFE_EMIS,      
@VSNU_ASTO OUTPUT      
     
   If @@error <> 0      
   Begin      
    Rollback tran      
    Return      
   End          
  End    
    
     
  Insert Into TMDOCU_CLIE(      
  CO_EMPR, CO_TIPO_DOCU, NU_DOCU_CLIE, CO_CLIE, CO_MONE,       
  CO_TIPO_OPER, CO_COND_PAGO, FE_EMIS, CO_UNID_CONC, FE_VENC,       
  FE_PROG_COBR, FE_DESP, FA_TIPO_CAMB, IM_BRUT_AFEC, IM_BRUT_INAF,IM_BRUT_EXON,       
  IM_GAFI, IM_MORA, IM_FLET,ST_AFEC_GAFI, ST_AFEC_MORA, ST_AFEC_FLET,       
  ST_DCT1_BRAF, ST_DCT1_BRIN,ST_DCT1_BREX, ST_DCT1_GAFI, ST_DCT1_MORA, ST_DCT1_FLET,       
  ST_DCT2_BRAF, ST_DCT2_BRIN,ST_DCT2_BREX,ST_DCT2_GAFI, ST_DCT2_MORA, ST_DCT2_FLET,      
  PC_DCT1, IM_DCT1, PC_DCT2, IM_DCT2, CO_IMP1, PC_IMP1, IM_IMP1, CO_IMP2,       
  PC_IMP2, IM_IMP2, CO_IMP3,PC_IMP3, IM_IMP3, IM_TOTA, IM_PAGA, TI_DOCU_ORIG,       
  NU_DOCU_ORIG, FE_DOCU_ORIG, ST_CONT, DE_OBSE,CO_ESTA_DOCU, CO_USUA_CREA,FE_USUA_CREA, CO_USUA_MODI,      
FE_USUA_MODI,       
  CO_VEND, CO_LIQU, TI_FACT, CO_MODE_DIST, TI_CLIE,     
  NU_ASTO)      
  Values  ( @ISCO_EMPR,  @ISCO_TIPO_DOCU, @ISNU_DOCU_CLIE, @ISCO_CLIE,      
  @ISCO_MONE, @ISCO_TIPO_OPER, @ISCO_COND_PAGO,  @IDFE_EMIS, @ISCO_UNID_CONC,       
  @IDFE_VENC, @IDFE_PROG_COBR, @IDFE_DESP, @INFA_TIPO_CAMB, @INIM_BRUT_AFEC,      
  @INIM_BRUT_INAF, @INIM_BRUT_EXON,@INIM_GAFI, @INIM_MORA, @INIM_FLET, @ISST_AFEC_GAFI,       
  @ISST_AFEC_MORA, @ISST_AFEC_FLET, @ISST_DCT1_BRAF, @ISST_DCT1_BRIN,@ISST_DCT1_BREX,       
  @ISST_DCT1_GAFI, @ISST_DCT1_MORA, @ISST_DCT1_FLET, @ISST_DCT2_BRAF,       
  @ISST_DCT2_BRIN,@ISST_DCT2_BREX, @ISST_DCT2_GAFI, @ISST_DCT2_MORA, @ISST_DCT2_FLET,       
  @INPC_DCT1, @INIM_DCT1, @INPC_DCT2, @INIM_DCT2, @ISCO_IMP1, @INPC_IMP1,       
  @INIM_IMP1, @ISCO_IMP2, @INPC_IMP2, @INIM_IMP2, @ISCO_IMP3, @INPC_IMP3,       
  @INIM_IMP3, @INIM_TOTA, @INIM_PAGA_REND, @ISCO_TIDO_ORIG, @ISNU_DOCU_ORIG,      
  @IDFE_DOCU_ORIG, 'N' , @ISDE_OBSE,'ACT', @ISCO_USUA_MODI,GETDATE(), @ISCO_USUA_MODI, GETDATE(),    
  @ISCO_VEND, @ISCO_LIQU, @ISTI_FACT, @ISCO_MODE_DIST, @VSTI_CLIE,     
  @VSNU_ASTO)      
      
  If @@error <> 0      
  Begin      
   Rollback tran      
   Return      
  End          
     
  End      
 Else      
 Begin      
  Update  TMDOCU_CLIE      
  Set  CO_MONE = @ISCO_MONE,      
  CO_COND_PAGO = @ISCO_COND_PAGO,      
  FE_EMIS = @IDFE_EMIS,      
  FE_VENC = @IDFE_VENC,      
  FE_PROG_COBR = @IDFE_PROG_COBR,      
  FE_DESP = @IDFE_DESP,       
  FA_TIPO_CAMB = @INFA_TIPO_CAMB,      
  IM_BRUT_AFEC = @INIM_BRUT_AFEC,      
  IM_BRUT_INAF = @INIM_BRUT_INAF,   
  IM_BRUT_EXON = @INIM_BRUT_EXON,         
  IM_GAFI  = @INIM_GAFI,      
  IM_MORA = @INIM_MORA,      
  IM_FLET =  @INIM_FLET,      
  ST_AFEC_GAFI = @ISST_AFEC_GAFI,      
  ST_AFEC_MORA = @ISST_AFEC_MORA,      
  ST_AFEC_FLET = @ISST_AFEC_FLET,      
  ST_DCT1_BRAF = @ISST_DCT1_BRAF,      
  ST_DCT1_BRIN = @ISST_DCT1_BRIN,      
  ST_DCT1_BREX = @ISST_DCT1_BREX,      
ST_DCT1_GAFI = @ISST_DCT1_GAFI,      
  ST_DCT1_MORA = @ISST_DCT1_MORA,      
  ST_DCT1_FLET = @ISST_DCT1_FLET,      
  ST_DCT2_BRAF = @ISST_DCT2_BRAF,      
  ST_DCT2_BRIN = @ISST_DCT2_BRIN,  
  ST_DCT2_BREX = @ISST_DCT2_BREX,          
  ST_DCT2_GAFI = @ISST_DCT2_GAFI,      
  ST_DCT2_MORA = @ISST_DCT2_MORA,      
  ST_DCT2_FLET = @ISST_DCT2_FLET,      
  PC_DCT1 = @INPC_DCT1,      
  IM_DCT1 = @INIM_DCT1,      
  PC_DCT2 = @INPC_DCT2,      
  IM_DCT2 = @INIM_DCT2,      
  CO_IMP1 = @ISCO_IMP1,      
  PC_IMP1 = @INPC_IMP1,      
  IM_IMP1 = @INIM_IMP1,      
  CO_IMP2 = @ISCO_IMP2,      
  PC_IMP2 = @INPC_IMP2,      
  IM_IMP2 = @INIM_IMP2,      
  CO_IMP3 = @ISCO_IMP3,      
  PC_IMP3 = @INPC_IMP3,      
  IM_IMP3 = @INIM_IMP3,      
  IM_TOTA = @INIM_TOTA,      
  TI_DOCU_ORIG = @ISCO_TIDO_ORIG,      
  NU_DOCU_ORIG = @ISNU_DOCU_ORIG,      
  FE_DOCU_ORIG = @IDFE_DOCU_ORIG,      
  DE_OBSE =  RTRIM(@ISDE_OBSE),      
  CO_UNID_CONC  = @ISCO_UNID_CONC,      
  CO_VEND = @ISCO_VEND,      
  CO_LIQU = @ISCO_LIQU,      
  TI_FACT = @ISTI_FACT,                                 
  CO_USUA_MODI =  @ISCO_USUA_MODI,      
  FE_USUA_MODI  = Getdate()      
  Where   CO_EMPR = @ISCO_EMPR      
  And CO_CLIE = @ISCO_CLIE      
  And CO_TIPO_DOCU = @ISCO_TIPO_DOCU      
  And NU_DOCU_CLIE = @ISNU_DOCU_CLIE      
     
  If @@error <> 0      
  Begin      
   Rollback tran      
   Return      
  End          
     
 End              
     
      
Commit Tran      
      
/*-------------------------- Fin ------------------------------*/  
GO
